//
//  ArkViewController.m
//  Ark
//
//  Created by Daniel on 18/04/2011. Edited and improved by Tony, 20/05/2011.
//  Copyright 2011 Daniel Webb. All rights reserved.
//

#import "ArkViewController.h"
#import "XMLDocument.h"
#import "XMLElement.h"


@implementation ArkViewController


@synthesize theResult;
@synthesize generateArk;
@synthesize theList;




-(IBAction) showArk:(id)sender {

	theResult.text= [theList objectAtIndex:arc4random()%[theList count]];
	
}


- (void) addToList:(NSString*)item {
	[theList addObject:item];
	//NSLog(@"Added %@", [theList count]);
}


- (void) recursivelyLogXMLContents:(XMLElement*)paramElement{
	
	//NSLog(@"Element Name = %@", paramElement.name);
	
	if ([paramElement.name isEqual:@"en"]) {
		[self addToList:paramElement.text];
	}
	
	NSLog(@"Element Text = %@", paramElement.text);
	
	//NSLog(@"Number of Attributes = %lu", 
	//	  (unsigned long)[paramElement.attributes count]);
	
	//if (paramElement.parent != nil){
	//	NSLog(@"Parent Element Name = %@", paramElement.parent.name);
	//}*/
	
	if ([paramElement.attributes count]){
		NSLog(@"Attributes Dictionary = %@", paramElement.attributes);
	}
	
	//NSLog(@"Number of children elements = %lu", 
	//	  (unsigned long)[paramElement.children count]);
	
	
	
	for (XMLElement *child in paramElement.children){
		[self recursivelyLogXMLContents:child];
		
		//theResult.text= (@"%@", paramElement.text);
	}

	

}


//********** MOTION BEGAN **********

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
	if (motion==UIEventSubtypeMotionShake) {
		theResult.text= [theList objectAtIndex:arc4random()%[theList count]];
		NSLog(@"Shake detected");
	}
}


- (BOOL)canBecomeFirstResponder {
	return YES; //For the shake event
}

- (void)viewDidAppear:(BOOL)animated {
    [self becomeFirstResponder];  // For the shake event
    [super viewDidAppear: animated];
}




/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	
	NSLog(@"Initializing!!");
	NSMutableArray *childrenArray = [[NSMutableArray alloc] init];
	theList = [childrenArray mutableCopy];
	[childrenArray release];
	NSLog(@"Array built!");
	
	NSBundle *mainBundle = [NSBundle mainBundle];
	
	NSString *xmlFilePath = [mainBundle pathForResource:@"actList"
												 ofType:@"xml"];
	
	XMLDocument     *document = [[XMLDocument alloc] init];
	
	if ([document parseLocalXMLWithPath:xmlFilePath] == YES){
		[self recursivelyLogXMLContents:document.rootElement];
	} else {
		NSLog(@"Could not parse the XML file.");
	}
	
	[document release];
	
	

 
}	 
	 
	 

	 
	 

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	[super viewDidUnload];
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[theResult release];
	[generateArk release];
	[theList release];
    [super dealloc];
}

@end
